package com.zhengke.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.zhengke.common.util.tools.LoadProperties;
import com.zhengke.pojo.Experie;
import com.zhengke.service.ExperieService;

@Controller
public class ExperieController {

	@Autowired
	private ExperieService service;
	
/**
 * ssx
 *   到体验商品列表 
 * */	
@RequestMapping("toExperieList.do")	
public String querys(ModelMap mod) {
	mod.put("tmp",new Date());
	return "tv/experie/experieList";
} 
	
/**
 *  商品列表
 * */
@ResponseBody
@RequestMapping("experieData.do")
public void	queryExperieList(String servicePointId,HttpServletResponse response,HttpServletRequest request) {
	
	List<Experie> queryList = service.queryExperieList(servicePointId);
	try {
		List<Map<String,Object>> rtnList = new ArrayList<Map<String,Object>>();
		String notFoundPic =  LoadProperties.findValue("losePicForList");
		for(Experie list : queryList){
			Map<String,Object> temp = new HashMap<String,Object>();
			String picPath = list.getThumbPicPath();
			if(picPath==null||"".equals(picPath))
				picPath = request.getSession().getServletContext().getRealPath(notFoundPic);
			else{
				File f = new File(picPath);
				if(!f.exists())
					picPath = request.getSession().getServletContext().getRealPath(notFoundPic);
			}
		//	System.out.println(picPath+"------------1");
			temp.put("goodsId",list.getGoodsId());
			temp.put("picPath", picPath);
			temp.put("goodsName", list.getGoodsName());
			temp.put("goodsPromotionPrice","￥"+list.getGoodsPrice());
			temp.put("type",list.getIsNormal());
			rtnList.add(temp);
		}
		
		response.setCharacterEncoding("utf-8");
		response.setHeader("Pragma","No-cache"); 
		response.setHeader("Cache-Control","no-cache"); 
		response.setDateHeader("Expires", 0);  
		response.getWriter().print("var dataList="+JSONArray.toJSON(rtnList));

	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}



/**
 * 到商品详情页面
 * @return
 */
@RequestMapping("toGoodsDetail.do")
public String togoodsDetail(int goodsId,String orderNumber,String type,String pageTitle,ModelMap model,HttpServletRequest request) {
	model.put("tmp", new Date());
	try{
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	
	    list = service.queryExperie(goodsId);
		
		for(Map<String, Object> obj:list){
			String infoPicPath =obj.get("info_picPath").toString();
			String notInfoPic =  LoadProperties.findValue("loseInfoPicForGoodsDetail");
			if(infoPicPath==null||"".equals(infoPicPath))
				infoPicPath = request.getSession().getServletContext().getRealPath(notInfoPic);
			else{
				File f = new File(infoPicPath);
				if(!f.exists())
					infoPicPath = request.getSession().getServletContext().getRealPath(notInfoPic);
			}
			
			List<String> rtnList = new ArrayList<String>();
			
			if(obj.get("picPath1")!=null&&!"".equals(obj.get("picPath1")))
				rtnList.add(obj.get("picPath1").toString());
			if(obj.get("picPath2")!=null&&!"".equals(obj.get("picPath2")))
				rtnList.add(obj.get("picPath2").toString());
			if(obj.get("picPath3")!=null&&!"".equals(obj.get("picPath3")))
				rtnList.add(obj.get("picPath3").toString());
			if(obj.get("picPath4")!=null&&!"".equals(obj.get("picPath4")))
				rtnList.add(obj.get("picPath4").toString());
			if(obj.get("picPath5")!=null&&!"".equals(obj.get("picPath5")))
				rtnList.add(obj.get("picPath5").toString());
			
			if(rtnList.size()==0){
				String notFoundPic =  LoadProperties.findValue("losePicForGoods");
				rtnList.add(request.getSession().getServletContext().getRealPath(notFoundPic));
			}
			
			
			String shippingFee  = obj.get("shipping_fee")==null?"":obj.get("shipping_fee").toString();
			double s = Double.parseDouble(shippingFee)*(2);
			model.put("shippingFee", s);//运费
			
			model.put("goodsId", goodsId);
			model.put("picList",rtnList);
			model.put("picList", rtnList);
			model.put("goodsName", obj.get("goods_name"));
			model.put("goodsPrice", "￥"+obj.get("goods_price"));//打折价
			model.put("deliveryWay", "门店自提");//收货方式
			model.put("supplierName", obj.get("supplier_name"));//供应商名称
			model.put("supplierId", obj.get("supplier_id"));//供应商id
			model.put("infoPicPath", infoPicPath);//介绍图片
			model.put("awardedIntegral", obj.get("awarded_integral"));//赠送积分
			model.put("specifications", obj.get("specifications"));//商品规格
		}
		if(orderNumber==null||"".equals(orderNumber))
			model.put("orderNumber", 1);
		else
			model.put("orderNumber", orderNumber);
		
		//图片查询滚动时间
			model.put("time", "1000");//轮播图片滚动时间
		if("0".equals(type)||"2".equals(type)){
			model.put("show", "0");//显示类型
		}else{
			model.put("show", "1");//显示类型
		}
		model.put("type", type);//商品类型
		
		
	}catch(Exception e){
		e.printStackTrace();
	}		
	return "tv/experie/experieDetails";
} 
/**
 *   controller请求转发
 * */
/*
@RequestMapping("toGoodsDetail.do")	
public ModelAndView querys1() {
	ModelAndView mv = new ModelAndView();
	mv.setViewName("forward:toDiscountGoodsDetail.do");
	return mv;
} 
*/

}
